package com.stbu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stbu.bean.EmployeeBean;
import com.stbu.mapper.EmployMapper;
import com.stbu.utils.EmploymentModel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class EmployerService extends ServiceImpl<EmployMapper, EmployeeBean> {

    private final EmployMapper employMapper;

    //根据员工Id查询员工数据
    public EmployeeBean getEmployerById(int eid) {
       EmployeeBean employeeBean =  employMapper.selectById(eid);
       return employeeBean;
    }

    public List<EmployeeBean> getAllEmployer() {
        QueryWrapper queryWrapper = new QueryWrapper<EmployeeBean>();
        return employMapper.selectList(queryWrapper);
    }

    //返回带有分页的数据
    public Page<EmployeeBean> getAllList(EmploymentModel param) {
        EmployeeBean  employeeBean = new EmployeeBean();
        employeeBean.setEid(param.getEid());
        employeeBean.setEname(param.getEname());
        employeeBean.setEage(param.getEage());
        employeeBean.setEaddr(param.getEaddr());
        employeeBean.setSex(param.getSex());

        Page page = new Page();
        page.setCurrent(param.getCurrent());
        page.setSize(page.getSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(employeeBean);
       Page<EmployeeBean> employeeBeanPage =  employMapper.selectPage(page,queryWrapper);
       return employeeBeanPage;
    }

    //添加员工信息
    public int addEmployee(EmploymentModel param) {
        EmployeeBean  employeeBean = new EmployeeBean();
        employeeBean.setEid(param.getEid());
        employeeBean.setEname(param.getEname());
        employeeBean.setEage(param.getEage());
        employeeBean.setEaddr(param.getEaddr());
        employeeBean.setSex(param.getSex());
        try {
            return employMapper.insert(employeeBean);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    //修改员工信息
    public int modifyEmployee(EmploymentModel param) {
        if (param.getEid() ==null || param.getEid()<=0){
            return -1;
        }
        EmployeeBean  employeeBean = new EmployeeBean();
        employeeBean.setEid(param.getEid());
        employeeBean.setEname(param.getEname());
        employeeBean.setEage(param.getEage());
        employeeBean.setEaddr(param.getEaddr());
        employeeBean.setSex(param.getSex());
        employeeBean.setIdcard(param.getIdcard());
       try {
          return employMapper.updateById(employeeBean);
       } catch (Exception e) {
           e.printStackTrace();
           return -1;
       }
    }

    public int delEmployeeById(int id) {
        return employMapper.deleteById(id);
    }

}
